package bdeenyy.threevector.ui;

import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;
import javafx.scene.shape.Polygon;
import javafx.scene.text.Text;

public class VectorDrawer {

    public static void drawTriangleAndVector(Pane pane, double centerX, double centerY, double sideLength, double[] vector, String name, double resultLength, double azimuth, double scale, boolean isClockwise) {
        double height = Math.sqrt(3) / 2 * sideLength;
        double halfSide = sideLength / 2;

        double x1 = centerX;
        double y1 = centerY - (2.0 / 3.0) * height;
        double x2 = centerX - halfSide;
        double y2 = centerY + (1.0 / 3.0) * height;
        double x3 = centerX + halfSide;
        double y3 = centerY + (1.0 / 3.0) * height;

        double radians = Math.toRadians(azimuth);
        double cosAngle = Math.cos(radians);
        double sinAngle = Math.sin(radians);

        double[] rotatedX = new double[3];
        double[] rotatedY = new double[3];

        double[] originalX = {x1, x2, x3};
        double[] originalY = {y1, y2, y3};

        for (int i = 0; i < 3; i++) {
            rotatedX[i] = centerX + (originalX[i] - centerX) * cosAngle - (originalY[i] - centerY) * sinAngle;
            rotatedY[i] = centerY + (originalX[i] - centerX) * sinAngle + (originalY[i] - centerY) * cosAngle;
        }

        drawTriangle(pane, rotatedX, rotatedY);
        addPerpendiculars(pane, rotatedX, rotatedY, isClockwise);


        drawVector(pane, centerX, centerY, vector, name, resultLength, radians, scale);
    }

    private static void drawTriangle(Pane pane, double[] x, double[] y) {
        Polygon triangle = new Polygon(x[0], y[0], x[1], y[1], x[2], y[2]);
        triangle.setStroke(Color.BLACK);
        triangle.setFill(Color.TRANSPARENT);
        pane.getChildren().add(triangle);
    }

    private static void addPerpendiculars(Pane pane, double[] x, double[] y, boolean isClockwise) {
        String[] labels = isClockwise ? new String[]{"ОП2", "ОП1", "ОП3"} : new String[]{"ОП3", "ОП1", "ОП2"};

        double offset = 1;  // Смещение текста

        for (int i = 0; i < 3; i++) {
            int j = (i + 1) % 3;
            double midX = (x[i] + x[j]) / 2;
            double midY = (y[i] + y[j]) / 2;
            double dx = y[j] - y[i];
            double dy = x[i] - x[j];
            double length = 20.0 / Math.sqrt(dx * dx + dy * dy);  // Увеличиваем длину перпендикуляра до 20 пикселей
            double perpX = midX - dx * length;
            double perpY = midY - dy * length;

            Line perpendicular = new Line(midX, midY, perpX, perpY);
            perpendicular.setStroke(Color.LIGHTGRAY);  // Меняем цвет на светло-серый
            pane.getChildren().add(perpendicular);

            // Определяем смещение текста в зависимости от направления перпендикуляра
            double textXOffset = (perpX - midX) * offset;
            double textYOffset = (perpY - midY) * offset;

            Text label = new Text(perpX + textXOffset, perpY + textYOffset, labels[i]);
            label.setFill(Color.LIGHTGRAY);  // Меняем цвет текста на светло-серый
            pane.getChildren().add(label);
        }
    }

    private static void drawVector(Pane pane, double centerX, double centerY, double[] vector, String name, double resultLength, double radians, double scale) {
        double vectorEndX = centerX + vector[0] * scale;
        double vectorEndY = centerY - vector[1] * scale;

        double cosAngle = Math.cos(radians);
        double sinAngle = Math.sin(radians);

        double rotatedEndX = centerX + (vectorEndX - centerX) * cosAngle - (vectorEndY - centerY) * sinAngle;
        double rotatedEndY = centerY + (vectorEndX - centerX) * sinAngle + (vectorEndY - centerY) * cosAngle;

        Line resultVectorLine = new Line(centerX, centerY, rotatedEndX, rotatedEndY);
        resultVectorLine.setStroke(Color.RED);
        resultVectorLine.setStrokeWidth(2);

        double arrowLength = 10;
        double arrowAngle = Math.toRadians(20);

        double lineAngle = Math.atan2(rotatedEndY - centerY, rotatedEndX - centerX);

        double arrowX1 = rotatedEndX - arrowLength * Math.cos(lineAngle - arrowAngle);
        double arrowY1 = rotatedEndY - arrowLength * Math.sin(lineAngle - arrowAngle);
        double arrowX2 = rotatedEndX - arrowLength * Math.cos(lineAngle + arrowAngle);
        double arrowY2 = rotatedEndY - arrowLength * Math.sin(lineAngle + arrowAngle);

        Line arrow1 = new Line(rotatedEndX, rotatedEndY, arrowX1, arrowY1);
        arrow1.setStroke(Color.RED);
        arrow1.setStrokeWidth(2);

        Line arrow2 = new Line(rotatedEndX, rotatedEndY, arrowX2, arrowY2);
        arrow2.setStroke(Color.RED);
        arrow2.setStrokeWidth(2);

        double nameDouble = Double.parseDouble(name);

        Text vectorLabel = new Text(centerX - 30, centerY - 100, String.format("отм + %.3f м", nameDouble));
        vectorLabel.setFill(Color.BLACK);

        Text resultLabel = new Text(rotatedEndX + 10, rotatedEndY, String.format("%.2f мм", resultLength));
        resultLabel.setFill(Color.RED);

        pane.getChildren().addAll(resultVectorLine, arrow1, arrow2, vectorLabel, resultLabel);
    }
}